package org.hu.controller;

import org.hu.model.Department;
import org.hu.model.Employee;
import org.hu.service.DepartmentService;
import org.hu.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 注册
 * @author Htrees
 */
@Controller
public class RegisterController {
    @Autowired
    DepartmentService departmentService;
    @Autowired
    EmployeeService employeeService;

    /**
     * 注册页面
     * @param model
     * @return register
     */
    @RequestMapping("/register")
    public String register(Model model){
        List<Department> deps = departmentService.getAllDeps();
        model.addAttribute("deps",deps);
        return "register";
    }

    /**
     * 执行注册
     * @param employee 员工对象
     * @param model
     * @return /
     */
    @PostMapping("/doReg")
    public String doReg(Employee employee,Model model){
        Integer result = employeeService.doReg(employee);
        if(result==1){
            return "redirect:/";
        }else{
            model.addAttribute("employee",employee);
            model.addAttribute("error","用户名已存在");
            return "forward:/register";
        }

    }
}
